﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace CommonSLT
{
    public partial class NavControl : DevExpress.XtraEditors.XtraUserControl
    {
        #region private variants
        private int index = 0;
        private int totalRow = 0;
        #endregion private variants
        //==========================================================================

        #region public variants
        public event MyNavigatorEventHandler OnNavigate = null;
        #endregion public variants
        //==========================================================================

        public NavControl()
        {
            InitializeComponent();
            this.btnBackward.Click += new EventHandler(btnBackward_Click);
            this.btnBack.Click += new EventHandler(btnBack_Click);
            this.btnNext.Click += new EventHandler(btnNext_Click);
            this.btnForward.Click += new EventHandler(btnForward_Click);
            this.cmbPageNumber.SelectedIndexChanged += new EventHandler(cmbPageNumber_SelectedIndexChanged);
            this.cmbPageNumber.TextChanged += new EventHandler(cmbPageNumber_TextChanged);
        }

        public void SetInfo(int index, int totalRow)
        {
            this.index = index;
            this.totalRow = totalRow;
            this.cmbPageNumber.Properties.Items.Clear();
            for (int i = 1; i < totalRow; i++)
                this.cmbPageNumber.Properties.Items.Add("Trang " + i.ToString() + " / " + totalRow.ToString());
            this.cmbPageNumber.SelectedIndex = this.index - 1;
            SetBtnStatus();
        }

        public void SetInfo(int index)
        {
            this.index = index;
            this.cmbPageNumber.SelectedIndex = this.index - 1;
            SetBtnStatus();
        }

        private void SetBtnStatus()
        {
            if (this.totalRow == 0)
            {
                this.btnBackward.Enabled = false;
                this.btnBack.Enabled = false;
                this.btnNext.Enabled = false;
                this.btnForward.Enabled = false;
            }
            else
            {
                this.btnBackward.Enabled = (this.index > 1);
                this.btnBack.Enabled = (this.index > 1);
                this.btnNext.Enabled = (this.index < this.totalRow);
                this.btnForward.Enabled = (this.index < this.totalRow);
            }
        }

        private void btnBackward_Click(object sender, EventArgs e)
        {
            MyNavigatorEventArgs evt = new MyNavigatorEventArgs(NavAction.Backward, 1);
            if (OnNavigate != null)
                OnNavigate(this, evt);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            MyNavigatorEventArgs evt = new MyNavigatorEventArgs(NavAction.Back, (this.index - 1));
            if (OnNavigate != null)
                OnNavigate(this, evt);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            MyNavigatorEventArgs evt = new MyNavigatorEventArgs(NavAction.Next, (this.index + 1));
            if (OnNavigate != null)
                OnNavigate(this, evt);
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            MyNavigatorEventArgs evt = new MyNavigatorEventArgs(NavAction.Forward, this.totalRow);
            if (OnNavigate != null)
                OnNavigate(this, evt);
        }

        private void cmbPageNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetInfo(this.cmbPageNumber.SelectedIndex + 1);
            MyNavigatorEventArgs evt = new MyNavigatorEventArgs(NavAction.ChangeIndex, this.index);
            if (OnNavigate != null)
                OnNavigate(this, evt);
        }

        private void cmbPageNumber_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string s = cmbPageNumber.Text;
                if (s.ToLower().StartsWith("trang"))
                    s = s.Remove(0, 5);
                string[] array_s = s.Split('/');
                int i = int.Parse(array_s[0]);
                if (i > 0 && i <= this.totalRow)
                {
                    SetInfo(i);
                    MyNavigatorEventArgs evt = new MyNavigatorEventArgs(NavAction.ChangeIndex, this.index);
                    if (OnNavigate != null)
                        OnNavigate(this, evt);
                }
                else
                    SetInfo(this.index);
            }
            catch
            {
                SetInfo(this.index);
            }
        }
    }
}